package com.ttdc.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ttdc.pojo.OrderDetails;
import com.ttdc.pojo.OrderDetailsExample;
import com.ttdc.pojo.OrderList;
import com.ttdc.service.OrderDetailsService;
import com.ttdc.service.OrderListService;

@Controller
public class OrderDetailsController {
	@Autowired
	
	private OrderDetailsService orderDetailsService;
	private OrderDetailsExample odexample;
	
	@RequestMapping("/findAllOd")
	public String findAllOrderDetails(Model model){
		List<OrderDetails> list =orderDetailsService.selectByAll();
		model.addAttribute("list", list);
		return "dc";
	}
	@SuppressWarnings("finally")
	@RequestMapping("/saveOrderDetails")
	public String saveOrderDetails(OrderDetails record,Model model,BindingResult bindingResult){
		try {
			orderDetailsService.insertSelective(record);
			List<OrderDetails> list = orderDetailsService.selectByAll();
			model.addAttribute("list", list);

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			return "dc";
		}
	}
	@RequestMapping("/deleteOrderDetails")
	public String removeOrderDetails(@RequestParam("odId") Integer odId,Model model){
		orderDetailsService.deleteByPrimaryKey(odId);
		return "redirect:/saveOrderDetails";
	}
	@RequestMapping("/tupdateOrderDetails")
	public String updateByPrimaryKeySelective(Integer odId,Model model){
		OrderDetails od = orderDetailsService.selectByPrimaryKey(odId);
		model.addAttribute("od", od);
		return "Odupdate";
	}
	@RequestMapping("/updateOrderDetails")
	public String replaceOrderDetails(OrderDetails record){
		orderDetailsService.updateByPrimaryKey(record);
		return "redirect:/findAllOd";
	}
	@RequestMapping("/selectByOdId")
	public String selectByPrimaryKey(OrderDetails orderDetails,Model model){
		odexample = new OrderDetailsExample();
		OrderDetailsExample.Criteria c = odexample.createCriteria();
		if (orderDetails == null) {
			return "Odtest";
		} else {
			if (orderDetails.getOdId() != null) {
				System.out.println(orderDetails.getOdId());

				orderDetails = orderDetailsService.selectByPrimaryKey(orderDetails.getOdId());
				model.addAttribute("list", orderDetails);
			}
			return "Odtest";
			
	}
	}
	@RequestMapping("/selectAllOdMag")
	public String selectAll(Model model){
		odexample = new OrderDetailsExample();
		OrderDetailsExample.Criteria criteria = odexample.createCriteria();
		criteria.andOdIdIsNotNull();
		List<OrderDetails> list = orderDetailsService.selectByExample(odexample);
		model.addAttribute("list", list);
		return "Odtest";
	}
}
